﻿using System;
using NUnit.Framework;

namespace BlindData
{
	/// <summary>
	/// Base implementation of NUnit test fixtures written in the Given-When-Then style
	/// </summary>
	[TestFixture]
	public abstract class Behavior
	{
		/// <summary>
		/// Initializes this text fixture by executing Given and When
		/// </summary>
		[TestFixtureSetUp]
		public void SetUp()
		{
			Given();

			When();
		}

		/// <summary>
		/// When implemented in a derived class, sets up the state for the tested action
		/// </summary>
		protected abstract void Given();

		/// <summary>
		/// When implemented in a derived class, performs the tested action
		/// </summary>
		protected abstract void When();

		/// <summary>
		/// Executes the action and returns any exceptions that where thrown.
		/// </summary>
		/// <param name="action">The action.</param>
		/// <returns></returns>
		public static Exception ExpectException(Action action)
		{
			Exception exception = null;

			try
			{
				action();
			}
			catch (Exception e)
			{
				exception = e;
			}

			return exception;
		}
	}
}
